import React from 'react';
import {Switch, Route} from 'dva/router';

let childrenConfig = [];
const getChildrenConfig = (config, path) => {
    config.some(item => {
        if (item.path === path) {
            childrenConfig = item.children;
            return true;
        } else {
            if (item.children && item.children.length) {
                getChildrenConfig(item.children, path);
            }
            return false;
        }
    });
    return childrenConfig;
};

export default props => {
    const {_routesConfig} = React;
    const {routesKey} = props;
    const routesConfig = _routesConfig;
    let thanConfig = [];
    if (routesKey === '/') {
        thanConfig = routesConfig;
    } else {
        thanConfig = getChildrenConfig(routesConfig, routesKey);
    }
    return (
        <Switch>
            {thanConfig.map(({path, component}, index) => (
                <Route key={index} path={path} exact={path.substr(path.length - 1, 1) === '/'} component={component} />
            ))}
        </Switch>
    );
};
